$(document).ready(function(){
	// 自动加载默认页面
	// 获取传参 
	// 代理商id
	var getag = $('.pushdata input[name=getag]').val();
	// eui
	var geteui = $('.pushdata input[name=geteui]').val();
	// 应用标志
	var getapply = $('.pushdata input[name=getapply]').val();
	// 遍历选项卡
	$("li[role=presentation]").each(function(i){
		// 判断是否为链接过来的,是,根据传参获取数据
		if(getag&&geteui&&getapply){
			if($(this).children().attr("aria-controls")==getapply){
				$("li[role=presentation]").attr('class','');
				$("li[role=presentation]").children().attr('aria-expanded','false');
				$(this).attr('class','active');
				$(this).children().attr('aria-expanded','true');
				$("#myTabContent").children().attr('class','tab-pane fade');
				$("#"+getapply).attr('class','tab-pane fade active in');
				$.ajax({
					type:"post",
					url:"/admin/device/toajax",
					data:{
						"ag":getag,
						"eui":geteui,
						"application":getapply
					},
					headers: {
						'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
					},
					acync:false,
					success:function(str){
						$("#"+getapply).html(str);
					}
				});
			}
		}else{
			// 若不是链接跳转过来的,选择默认加载
			if($(this).children().attr("aria-expanded")=="true"){
				fun2(i);
			}
		}
		// 选择应用,点击事件,加载页面
		$(this).click(function(){
			fun2(i);
		});
	});
});

function fun2(i){
	// 获取选中的标志
	var apply = $("li[role=presentation]:eq("+i+")").children().attr("aria-controls");
	$.ajax({
		type:"post",
		url:"/admin/device/toajax",
		data:{"application":apply},
		headers:{
			'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
 		},
        async:false,
 		success:function(str){
 			$("#"+apply).html(str);
 		}
	});
}

function fun(lg,app){
	var lw_page;
	var ag = $('#'+app+' select[name=ag] option:selected').val();
	var eui = $('#'+app+' input[name=eui]').val();
	var area = $('#'+app+' input[name=area]').val();
	var building = $('#'+app+' input[name=building]').val();
	var room = $('#'+app+' input[name=room]').val();
	// 判断如果是form表单提交
	if(lg=='submit'){
		lw_page =1;
	}else{
		lw_page = lg;
	}
	$.ajax({
		type:"post",
		url:"/admin/device/toajax",
		data:{
			"ag":ag,
			"eui":eui,
			"area":area,
			"building":building,
			"room":room,
			"page":lw_page,
			"application":app
		},
        async:false,
		headers:{
			'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
		 	},
		 	success:function(str){
		 		$("#"+app).html(str);
		 	}
	});
}
function del(id,app) {
    $.ajax({
        type:'post',
        url:'/admin/device/del',
        async:false,
        data:{
            'id':id,
			'application':app
        },
		headers:{
			'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
		 	},
        dateType:'json',
        success:function (str) {
            var data = jQuery.parseJSON(str);
            if(data.status == '1'){
                fun(1,app);
            }
            return false;
        }
    });
}
